package com.ym.b2b.order.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.util.StringUtils;
import org.apache.commons.collections.ListUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import com.ym.b2b.commons.dao.BaseDao;
import com.ym.b2b.order.dao.EmShoppingCartDao;
import com.ym.b2b.order.sqlbean.EmSShoppingCart;
import com.zc.commons.base.Page;

@Repository
public class EmShoppingCartDaoImpl<T> extends BaseDao<T> implements EmShoppingCartDao {

    @Override
    public Object addShoppingCart(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        return insert("EmSShoppintCart.addShoppingCart", emSShoppingCart);
    }

    @Override
    public int updateShoppingCart(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        return update("EmSShoppintCart.updateShoppingCart", emSShoppingCart);
    }

    @Override
    public Page<EmSShoppingCart> searchPageShoppingCart(EmSShoppingCart emSShoppingCart, Page<EmSShoppingCart> page) {
        page.setTotalRow(searchPageShoppingCartNum(emSShoppingCart));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSShoppingCart);

        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EmSShoppintCart.searchPageShoppingCart", map));
        return page;
    }

    @Override
    public Long searchPageShoppingCartNum(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSShoppingCart);
        return (Long) find("EmSShoppintCart.searchPageShoppingCartNum", map);
    }

    @Override
    public Integer isExitShoppintCart(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        return (Integer) find("EmSShoppintCart.isExitShoppintCart", emSShoppingCart);
    }

    @Override
    public Double countShoppintCartTotalMoeny(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSShoppingCart);
        map.put("productCodes",getProductCodes(emSShoppingCart));
        return (Double) find("EmSShoppintCart.countShoppintCartTotalMoeny", map);
    }

    @Override
    public int deleteShoppingCart(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSShoppingCart);
        map.put("productCodes",getProductCodes(emSShoppingCart));

        return delete("EmSShoppintCart.deleteShoppingCart", map);
    }

    @Override
    public List<EmSShoppingCart> searchShoppingCart(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSShoppingCart);

        map.put("productCodes",getProductCodes(emSShoppingCart));

        map.put("startrow", 1);
        map.put("pagesize", 10000);
        map.put("pageable", false);
        return list("EmSShoppintCart.searchPageShoppingCart", map);
    }

    private String getProductCodes(EmSShoppingCart emSShoppingCart){
        //选中购物车中的商品代码
        String productCodes="";
        if(emSShoppingCart.getList()!=null){
            for(int i=0;i<emSShoppingCart.getList().size();i++){
                if(i>0)productCodes+=",";
                if(!StringUtils.isEmpty(emSShoppingCart.getList().get(i).getProductCode())){
                    productCodes+="'"+emSShoppingCart.getList().get(i).getProductCode()+"'";
                }
            }
        }
        return productCodes;
    }

    @Override
    public EmSShoppingCart showEmSShoppingCartOther(EmSShoppingCart emSShoppingCart) throws DataAccessException {
        return (EmSShoppingCart) find("EmSShoppintCart.showEmSShoppingCartOther", emSShoppingCart);
    }

    @Override
    public Integer countShoppingCartNum(String buyerCode) throws DataAccessException {

        return (Integer) find("EmSShoppintCart.countShoppingCartNum", buyerCode);
    }

}
